<template>
	<view class="page">
		<view class="top">
			<text>你好，</text>
			<text>欢迎使用小象通信</text>
		</view>
		<view class="form">
			<u-input v-model="userPhone" type="number" placeholder="请输入手机号"/>
			<u-input placeholder="请填写验证码"  type="number" v-model="code" class="code">
				<template slot="suffix">
					<u-code ref="uCode" @change="codeChange" :seconds="60" ></u-code>
					<text @tap="getCode" type="success" size="mini" >{{codeText}}</text>
				</template>
			</u-input>
			<view class="u-xie">
				<u-checkbox-group v-model="checked" placement="row" size="40"  iconSize="30">
					<u-checkbox label=""  name="name"></u-checkbox>
				</u-checkbox-group>
				<text>我已阅读并同意小象通信的
					<text class="u-bule" @click="goXieyi()" >《用户协议》</text>和
					<text class="u-bule" @click="goYingsi()">《隐私协议》</text>
				</text>
			</view>
			<u-button type="primary" text="登录" shape="circle" @click="sumbit"></u-button>
		</view>
		<u-popup :show="show" mode="center"  @close="close" @open="open" :round="10">
			<view class="title">欢迎使用小象通信</view>
			<view class="centent">我们非常重视您的个人信息和隐私保护，为了更好地保障您的权益，请您在使用产品前，认真阅读并充分理解《隐私政策》和《用户协议》，以便我们更好的为您服务。当您勾选按钮并点击“同意”时，即表示您已仔细阅读并同意《隐私政策》和《用户协议》条款。</view>
			<view class="u-xie">
				<u-checkbox-group v-model="isSessCheckbox" placement="row" size="40"  iconSize="30">
					<u-checkbox label="" name="name"></u-checkbox>
				</u-checkbox-group>
				<text>阅读并同意完整的
					<text class="u-bule" @click="goXieyi()" >《用户协议》</text>和
					<text class="u-bule"  @click="goYingsi()">《隐私协议》</text>
				</text>
			</view>
			<view class="btn-group">
				<u-button class="btn-close" text="取消" shape="circle" @click="close"></u-button>
				<u-button type="primary" text="确认" shape="circle" @click="sumbitEvent"></u-button>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data(){
			return{
				value:'',
				userPhone:'',
				tips:'获取验证码',
				show:false,
				code:'',
				isSessCheckbox:[],
				checked:[],
				codeText:'获取验证码'
			}
		},
		onShow() {
			this.show = false
			uni.hideLoading();
			if(!uni.getStorageSync('isSessCheckbox')){
				this.show = true
			}
		},
		methods:{
			goXieyi(){
				uni.navigateTo({
					url:'/pages/user/xieyi'
				})
			},
			goYingsi(){
				uni.navigateTo({
					url:'/pages/user/yingsi'
				})
			},
			
			getCode(){
				if(!this.$utils.checkPhone(this.userPhone)){
					uni.showToast({
						title: '手机号有误请重新输入',
						icon: 'none'
					})
					return
				}
				if (this.$refs.uCode.canGetCode) {
					uni.showLoading({
						title: '正在获取验证码'
					})
					this.$api.smsCode({userPhone:this.userPhone}).then(res=>{
						if(res.code === '200'){
							uni.hideLoading();
							// 这里此提示会被this.start()方法中的提示覆盖
							uni.$u.toast('验证码已发送');
							// 通知验证码组件内部开始倒计时
							this.$refs.uCode.start();
						}
					})
				} else {
				  uni.$u.toast('倒计时结束后再发送');
				}
				
			},
			close(){
				// #ifdef H5
				this.show = false
				// #endif
				//#ifdef APP-PLUS
				if (uni.getSystemInfoSync().platform === 'ios') {
				    plus.runtime.launchApplication({ action: 'QUIT' }); 
				}else {
					plus.runtime.quit();
				}
				// #endif
			},
			sumbitEvent(){
				if(this.isSessCheckbox.length === 0){
					uni.showToast({
						title: '请勾选协议！',
						icon: 'none'
					})
					return
				}
				this.show = false
				uni.setStorageSync('isSessCheckbox',true)
			},
			sumbit(){
				if(this.checked.length === 0){
					uni.showToast({
						title: '请勾选协议！',
						icon: 'none'
					})
					return
				}
				var params = {
					userPhone:this.userPhone,
					smsCode:this.code
				}
				this.$api.checkout(params).then(res=>{
					if(res.code === '200'){
						uni.setStorageSync('userPhone',this.userPhone)
						uni.setStorageSync("iccid",res.data.iccid)
						uni.setStorageSync("saleType",res.data.saleType)
						this.$utils.switchTab('/pages/index/index')
					}else{
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
					}
				})
			},
			open(){},
			codeChange(text){
				console.log(text)
				this.codeText = text;
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page{
		height: 100vh;
		background: #F4F6FA;
		.top{
			padding:66upx 0 180upx 26upx;
			color: #28292D;
			font-size:40upx;
			text{
				display: block;
				font-family: PingFangSC-Medium, PingFang SC;
				font-weight: 500;
			}
		}
		.form{
			margin: 0 26upx;
			.u-input{
				height:100upx;
				line-height: 100upx;
				border-bottom: 1px solid #E4E4E6;
				background: url('./../../static/img/icon01.png') no-repeat left center;
				background-size: 30upx 44upx;
				padding-left:52upx !important;
				::v-deep .u-input__content__subfix-icon {
					color: $u-primary;
					font-size: 15px;
				}
			}
			.code{
				background: url('./../../static/img/icon02.png') no-repeat left center;
				background-size: 34upx 40upx;
			}
		}
	}
	.u-bule{
		color:$uni-bule;
	}
	.u-xie{
		display: flex;
		margin-top: 22upx;
		color:#999999;
		font-size: 24upx;
		margin-bottom: 72upx;
	}
	::v-deep .u-popup {
		&__content{
			width:76%;
			text-align: center;
			padding:60upx 38upx;
			.title{
				font-size:40upx;
				color:#333333;
				font-family: PingFangSC-Medium, PingFang SC;
			}
			.centent{
				font-size:28upx;
				text-align: left;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #646668;
				line-height:58upx;
				margin-top: 36upx;
			}
		}
		.btn-group{
			justify-content: space-between;
			display: flex;
			.u-button{
				width:48%;
				height: 88upx;
			}
			.u-button--primary{
				background: linear-gradient(90deg, #50A9FF 0%, #1772FF 100%);
			}
			.btn-close{
				background: #F5F5F5;
			}
		}
	}
</style>